package com.tanhua.server.controller;
import com.tanhua.model.enums.ActionType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SmallVideosService;
import com.tanhua.server.service.UserFreezeService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;
    @Autowired
    private UserFreezeService userFreezeService;


    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 点赞视频
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") ObjectId videoId) {
        videosService.likeOrDislike(videoId, ActionType.LIKE);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消点赞视频
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") ObjectId videoId) {
        videosService.likeOrDislike(videoId, ActionType.DISLIKE);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity queryComments(@PathVariable("id") ObjectId videoId,
                                        @RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = videosService.queryComments(videoId, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 发布评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") ObjectId videoId,
                                   @RequestBody Map map) {
        //判断是否被冻结
        userFreezeService.checkUserStatus("2", UserHolder.getUserId());
        String comment = (String) map.get("comment");
        videosService.comments(videoId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 点赞评论
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeComment(@PathVariable("id") ObjectId commentId) {
        videosService.likeOrDislikeComment(commentId, ActionType.LIKE);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消点赞评论
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity dislikeComment(@PathVariable("id") ObjectId commentId) {
        videosService.likeOrDislikeComment(commentId, ActionType.DISLIKE);
        return ResponseEntity.ok(null);
    }
    /**
     * 关注视频用户
     *
     * @param focusUserId 被关注的用户id
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long focusUserId) {
        videosService.followOrNot(focusUserId, ActionType.LIKE);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注视频用户
     *
     * @param unFocusUserId 被取消关注的用户id
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long unFocusUserId) {
        videosService.followOrNot(unFocusUserId, ActionType.DISLIKE);
        return ResponseEntity.ok(null);
    }
}
